<?php
class FrontController extends Controller{
	var $site_info;
	function FrontController(){
		parent::Controller();
		global $site_info;
		$this->site_info = $site_info;
	}
	
	/**
	 * 展示
	 * @param string $name
	 */
	protected function display($name=null){
		if($name==null){
			//如果未指定名称，则使用app_act作为默认模板名称
			$name = APP.'_'.ACT;
		}
		define('TEMPLATE_NAME', $name);//此值用于挂件中用于获取对应的配置使用

		//检查是否指定使用哪个版本的模板
		if(isset($_REQUEST['tpl_id'])){
			$tpl_id = trim($_REQUEST['tpl_id']);
			define('TPL_VERSION', $tpl_id);
			$name .= '_' . $tpl_id;
		}
		
		$name .= '.' . TEMPLATE_TYPE;
		//初始化SEO数据
		$this->init_seo();

		$base_data = array(
			'domain' 		=> DOMAIN,
			'main_domain' 	=> MAIN_DOMAIN,
			'site_id'		=> SITE_ID,
			'page_title'	=> $this->page_title,
			'keywords'		=> $this->keywords,
			'description'	=> $this->description,
			'app'			=> APP,
			'act'			=> ACT,
			'site_lang'		=> LANGUAGE,
			'site' => $this->site_info,
		);
		$this->assign($base_data);
		$base_template_path = USER_TEMPLATE_PATH . ($this->module=='front' ? '' : $this->module . '/') . AGENT . '/';
		if(is_file($base_template_path . $name)){
			$this->template_dir = $base_template_path;
		}else{
			//如果指定的模板不存在，则使用系统默认的模板
			$this->template_dir = ROOT_PATH . '/protected/web/' . $this->module . '/template/';
		}
		if(!is_file($this->template_dir . $name)){
			//尝试重新生成
			if(isset($tpl_id)){
				$page_tree = new PageTree($tpl_id);
			}else{
				$page_mod = new PageModel();
				$page_config = $page_mod->get_by_attrs(array('site_id'=>SITE_ID, 'name'=>TEMPLATE_NAME, 'available'=>tree));
				$page_tree = new PageTree($page_config, $page_mod);
			}
			$page_tree->save();
			//生成后会保存html，直接返回就可以了
			echo $this->view->compile($page_tree->gen_template());
			$this->view->free();//释放
			return;
			//throw new SmartyException('模板不存在：' . $this->template_dir . $name);
		}
		$this->view->template_dir = $this->template_dir;
		$this->view->display($name);
		$this->view->free();//释放
	}
}

?>